// 设置localStorage
export const setStorage = function(key, obj) {
  const json = JSON.stringify(obj)
  window.localStorage.setItem(key, json)
}

// 获取localStorage
export const getStorage = function(key) {
  const str = window.localStorage.getItem(key)
  if (!str) {
    return null
  }
  return JSON.parse(str)
}

// 移除localStorage
export const removeStorage = function(key) {
  window.localStorage.removeItem(key)
}


/**
 * 按钮权限
 * @param {*} el        [DOM]
 * @param {*} id   [菜单id]
 * @param {*} type   [权限类型] 1查看2添加3修改4删除5修改密码6编辑
 * @param {*} layout    [显示方式 inline、inline-block、block]
 * 使用方式:v-permission="{ id: 'accountBatchHandle', type: '3' }"
 */
export const getPermission = (el, id, type) => {
  const menuInfos = getStorage('menuInfos')
  const vuex = getStorage('vuex') // 使用插件 createPersistedState({key: 'masterVuex'})
  const isSuper = vuex.user && vuex.user.isSuper // 是否是超级管理员
  const data = flatData(menuInfos, 'childrens')

  const permission = data.filter(el => el.id === id).some(d => d.auth.includes(type))
  return permission || !!isSuper
}